package com.bestcem.xm.user.dao.impl;

import com.alibaba.excel.util.StringUtils;
import com.bestcem.xm.user.dao.UserSmtpDao;
import com.bestcem.xm.user.dao.mapper.UserSmtpMapper;
import com.bestcem.xm.user.entity.mysql.UserSmtp;
import com.bestcem.xm.user.entity.pojo.UserSmtpDO;
import com.bestcem.xm.user.util.business.DataBaseUtil;
import com.bestcem.xm.user.util.convert.UserSmtpConvert;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * 邮箱配置信息表服务
 *
 * @author qiongyu.cui <qiongyu.cui@idiaoyan.com>
 * @version v1.0
 * @date 2022/3/16 11:17
 */
@Repository
public class UserSmtpDaoImpl implements UserSmtpDao {

    @Resource
    private UserSmtpMapper userSmtpMapper;

    @Resource
    private UserSmtpConvert userSmtpConvert;

    @Override
    public String insert(UserSmtpDO userSmtpDO) {
        UserSmtp userSmtp = userSmtpConvert.do2Mysql(userSmtpDO);
        DataBaseUtil.setCreateAndUpdateTime(userSmtp);
        userSmtp.setSmtpId(DataBaseUtil.generateId());
        userSmtpMapper.insert(userSmtp);
        return userSmtp.getSmtpId();
    }

    @Override
    public String insertSelective(UserSmtpDO userSmtpDO) {
        UserSmtp userSmtp = userSmtpConvert.do2Mysql(userSmtpDO);
        DataBaseUtil.setCreateAndUpdateTime(userSmtp);
        if (StringUtils.isBlank(userSmtp.getSmtpId())) {
            userSmtp.setSmtpId(DataBaseUtil.generateId());
        }
        userSmtpMapper.insertSelective(userSmtp);
        return userSmtp.getSmtpId();
    }

    @Override
    public UserSmtpDO selectById(String smtpId) {
        return userSmtpConvert.mysql2Do(userSmtpMapper.selectByPrimaryKey(smtpId));
    }

    @Override
    public Integer updateSelectiveById(UserSmtpDO userSmtpDO) {
        UserSmtp userSmtp = userSmtpConvert.do2Mysql(userSmtpDO);
        DataBaseUtil.setUpdateTime(userSmtp);
        return userSmtpMapper.updateByPrimaryKeySelective(userSmtp);
    }

    @Override
    public Integer updateSelectiveByOrgId(UserSmtpDO userSmtpDO) {
        UserSmtp userSmtp = userSmtpConvert.do2Mysql(userSmtpDO);
        DataBaseUtil.setUpdateTime(userSmtp);
        return userSmtpMapper.updateSelectiveByOrgId(userSmtp);
    }

    @Override
    public Integer deleteByIds(List<String> ids, String orgId) {
        if (CollectionUtils.isEmpty(ids)) {
            return 0;
        }
        return userSmtpMapper.deleteByIds(ids, orgId);
    }

    @Override
    public Integer deleteBySelective(UserSmtpDO smtpDO) {
        return userSmtpMapper.deleteBySelective(smtpDO);
    }

    @Override
    public Long count(String orgId, List<Integer> validateStatus) {
        return userSmtpMapper.count(orgId, validateStatus);
    }

    @Override
    public List<UserSmtpDO> listByPage(Integer page, Integer size, Boolean descending, String sortBy, String orgId, List<Integer> validateStatus) {
        return userSmtpMapper.listByPage(orgId, validateStatus, (page - 1) * size, size,
                Boolean.TRUE.equals(descending) ? "desc" : "asc", sortBy);
    }

    @Override
    public Integer updateSelectiveByIdAndOrgId(String smtpId, String orgId, UserSmtpDO userSmtpDO) {
        UserSmtp userSmtp = userSmtpConvert.do2Mysql(userSmtpDO);
        userSmtp.setSmtpId(smtpId);
        userSmtp.setOrgId(orgId);
        DataBaseUtil.setUpdateTime(userSmtp);
        return userSmtpMapper.updateSelectiveByIdAndOrgId(userSmtp);
    }

    @Override
    public List<UserSmtpDO> selectBySelective(UserSmtpDO userSmtpDO) {
        return userSmtpConvert.mysqls2Dos(userSmtpMapper.selectBySelective(userSmtpConvert.do2Mysql(userSmtpDO)));
    }

}
